create trigger ${triggername} on ${tablename}
for insert,update,delete
as
	
begin
	declare @IsInsert bit,@IsUpdate bit,@IsDelete bit	
	declare @relation_id varchar(100)
	declare @rel_key_value varchar(100)
	declare @xml_str varchar(8000)

	set @relation_id = '${relation_id}'
	
	<#list cols?keys as myKey>
	declare @${myKey} <#if cols[myKey] == "char" ||  cols[myKey] == "varchar">${cols[myKey]}(100) <#else>${cols[myKey]}</#if>
	</#list>

	
	if exists(select 1 from inserted) and not exists(select 1 from deleted)
		set @IsInsert = 1
	else
		set @IsInsert = 0 
	if exists(select 1 from inserted) and exists(select 1 from deleted)
		set @IsUpdate = 1
	else
		set @IsUpdate = 0 
	if not exists(select 1 from inserted) and exists(select 1 from deleted)
		set @IsDelete = 1
	else
		set @IsDelete = 0 

	if(@IsInsert=1) 
	  begin
	    if <#rt>
		<#list cols?keys as myKey>
		UPDATE(${myKey}) <#if myKey_has_next> or </#if>  <#t>
		</#list>
		${""}<#lt>
		begin
			select @rel_key_value=convert(varchar,${relation_key_name}),<#rt>
			<#list cols?keys as myKey>
			@${myKey}=${myKey}<#if myKey_has_next>,<#else> from inserted</#if><#t>
			</#list>
			${""}<#lt>
			set @xml_str = '<data op="insert" relation_id="${relation_id}" rel_key="'+ltrim(rtrim(@rel_key_value))+'">'
			<#list cols?keys as myKey>
			if @${myKey} is not null
				set @xml_str = @xml_str + dbo.get_insert_node('${myKey}',<#if cols[myKey]=="datetime">convert(varchar,@${myKey},20))<#else>@${myKey})</#if>
			</#list>
			set @xml_str = @xml_str+'</data>'
		end
	  end
	if(@IsUpdate=1)
	  begin
	    if <#rt>
		<#list cols?keys as myKey>
		UPDATE(${myKey}) <#if myKey_has_next> or </#if>  <#t>
		</#list>
		${""}<#lt>
		begin
			declare @old_key_value varchar(100)
			select @old_key_value=convert(varchar,${relation_key_name})  from deleted
			select @${relation_key_name}=convert(varchar,${relation_key_name}),<#rt>
			<#list cols?keys as myKey>
			@${myKey}=${myKey}<#if myKey_has_next>,<#else> from inserted</#if><#t>
			</#list>
			${""}<#lt>
			set @xml_str = '<data op="update" relation_id="${relation_id}" rel_key="'+ltrim(rtrim(@old_key_value))+'">'
			<#list cols?keys as myKey>
			if @${myKey} is not null
				set @xml_str = @xml_str + dbo.get_insert_node('${myKey}',<#if cols[myKey]=="datetime">convert(varchar,@${myKey},20))<#else>@${myKey})</#if>
			</#list>
			set @xml_str = @xml_str+'</data>'
		end
	  end
	  
	if(@IsDelete=1)
		begin
			print 'deleting'
		end
	print dbo.submit_info('localhost','/datasource/servlet/forsql',8080,'valueChange',@xml_str)
end